package com.greyu.ysj;

import java.io.IOException;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.annotation.PreDestroy;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.greyu.ysj.utils.MyClient;
import com.mysql.jdbc.AbandonedConnectionCleanupThread;

@Component
public class AnnotationPreDestroy {
	
	 private final static Logger logger = Logger.getLogger(AnnotationPreDestroy.class);
	@PreDestroy
    public void destory() {
 
        System.out.println("我被销毁了*****************我是用的@PreDestory的方式******************");
       
        logger.info("[DriverMangerListner]:-------DriverManager deregisterDriver start...");
        //com.mysql.jdbc.AbandonedConnectionCleanupThread.uncheckedShutdown();
        Enumeration<Driver> enumeration = DriverManager.getDrivers();
        while (enumeration.hasMoreElements()) {
            try {
                DriverManager.deregisterDriver(enumeration.nextElement());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        logger.debug("[DriverMangerListner]:-------DriverManager deregisterDriver mid11111...");
        try {
        	AbandonedConnectionCleanupThread.checkedShutdown();
		} catch (Exception e) {
			System.out.println("@PreDestroy: " + e.getMessage());
            e.printStackTrace();
		}
        
      
        logger.debug("[DriverMangerListner]:-------DriverManager deregisterDriver mid...");
       
        try {
        	 MyClient.getInstance().getClient().dispatcher().executorService().shutdown();   //清除并关闭线程池
             MyClient.getInstance().getClient().connectionPool().evictAll();                 //清除并关闭连接池
			MyClient.getInstance().getClient().cache().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}                             //清除cache

       
        logger.debug("[DriverMangerListner]:-------DriverManager deregisterDriver end...");

        
//        try{
//	        DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());
//	    }catch(Exception e){
//	        e.printStackTrace();
//	    }
    }

}
